package com.cqs.example.netty.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.*;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author lixw
 * @Date 7/11/20 4:12 PM
 */
@Slf4j
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    private static AtomicInteger cnt = new AtomicInteger();
    private static AtomicInteger handlerCnt = new AtomicInteger();

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        log.info("第{}次,建立连接:{}",cnt.incrementAndGet(), ctx.channel().remoteAddress());
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        FullHttpRequest request = null;
        if (!(msg instanceof FullHttpRequest)) {
            log.warn("不能处理msg:{}",msg);
            return;
        }
        request = (FullHttpRequest) msg;
        String uri = request.uri();
        HttpMethod method = request.method();
//        ByteBuf content = request.content();
//        String body = content.toString(StandardCharsets.UTF_8);
//        log.info("第{}次 处理请求:method:{},uri:{},body:{}",handlerCnt.incrementAndGet(),method,uri,body);
        if (HttpMethod.GET.equals(method)){
            //METHOD 处理
        }
        if (HttpMethod.POST.equals(method)){
            //METHOD 处理
        }
        //响应
        ByteBuf data = Unpooled.buffer();
        data.writeBytes("ok".getBytes(StandardCharsets.UTF_8));
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,data);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain;charset=utf-8");
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
